package main

import (
	"fmt"
	"alex/ptp1588boundaryclock/com"
	"alex/ptp1588boundaryclock/datasets"
	"alex/ptp1588boundaryclock/datatypes"
	"alex/ptp1588boundaryclock/state"
	"alex/ptp1588boundaryclock/com/general"
	_"alex/msc/helper"
)

func main() {
	i := 4
	s := "bla " + fmt.Sprintf("%v", i) + " end"
	fmt.Println(s)
	str := "8080"
	str += ":"
	fmt.Println(str)
	var iTest uint32 = 3231748232
	fmt.Println(iTest)
	first := uint8((iTest >> 24))
	second := uint8(iTest >> 16)
	third := uint8(iTest >> 8)
	fourth := uint8(iTest)
	fmt.Println(first)
	fmt.Println(second)
	fmt.Println(third)
	fmt.Println(fourth)
	fmt.Println("========")
	var rev uint32
	rev = uint32(first)<<24|uint32(second) <<
			16|uint32(third)<<8|uint32(fourth)
	fmt.Println(rev)

	var b1 [3]byte
	var b2 [3]byte

	b1[0], b1[1], b1[2] = 0, 1, 2
	b2[0], b2[1], b2[2] = 3, 4, 5

	fmt.Printf("b1 %+v", b1)
	fmt.Printf("b2 %+v", b2)

	var msg com.MessageType
	msg = com.Announce
	var delay_reqmsg com.MessageType
	delay_reqmsg = com.Delay_Req
	var delay_respmsg com.MessageType
	delay_respmsg = com.Delay_Resp
	var pdelay_reqmsg com.MessageType
	pdelay_reqmsg = com.Pdelay_Req
	var pdelay_respmsg com.MessageType
	pdelay_respmsg = com.Pdelay_Resp
	var follow_upmsg com.MessageType
	follow_upmsg = com.Follow_Up
	var pdelay_resp_follow_upmsg com.MessageType
	pdelay_resp_follow_upmsg = com.Pdelay_Resp_Follow_Up

	fmt.Println("\n==> message start test")
	b1[0] = byte(msg)
	fmt.Println(msg)
	b1[0] = byte(delay_reqmsg)
	fmt.Println(delay_reqmsg)
	b1[0] = byte(delay_respmsg)
	fmt.Println(delay_respmsg)
	b1[0] = byte(pdelay_reqmsg)
	fmt.Println(pdelay_reqmsg)
	b1[0] = byte(pdelay_respmsg)
	fmt.Println(pdelay_respmsg)
	b1[0] = byte(follow_upmsg)
	fmt.Println(follow_upmsg)
	b1[0] = byte(pdelay_resp_follow_upmsg)
	fmt.Println(pdelay_resp_follow_upmsg)
	fmt.Println("==> message end test")

	vers := new(datasets.PortDS)
	vers.VersionNumber = 128

	b1[1] = vers.VersionNumber
	fmt.Println(b1)

	zahl := 0
	zahl += 1
	fmt.Println(zahl)
	zahl += (1<<1)
	zahl += (1<<2)
	fmt.Println(zahl)

	test := new(datasets.DefaultDS)
	fmt.Println(test)
	fmt.Println(*test)
	fmt.Println(&test)

	//time.Sleep(100 * time.Millisecond)

	/*	car1, car2 := new(stuff.Car), new(stuff.Car)
		car1.Id = 10
		car2.Id = 20
		go stuff.TestCar(*car1, 1, 500)
		go stuff.TestCar(*car2, 2, 10)*/

	//time.Sleep(10 * time.Second)
	fmt.Println("Exit")

	var signed int8
	var unsigned uint8

	signed = -127
	fmt.Println(signed)
	unsigned = uint8(signed)
	fmt.Println(unsigned)

	fmt.Println(int8(unsigned))

	fmt.Println("==========================================")
	defaultA := new(datasets.DefaultDS)
	defaultA.Priority1 = 13
	defaultA.Priority2 = 13
	defaultA.ClockIdentity = datatypes.ClockIdentity{0xFF, 0xFF, 0xA5, 0, 0, 0, 0, 0}
	defaultA.ClockQuality = datatypes.ClockQuality{248, 0x2a, 0}

	defaultB := new(datasets.DefaultDS)
	defaultB.Priority1 = 13
	defaultB.Priority2 = 13
	defaultB.ClockIdentity = datatypes.ClockIdentity{0xFF, 0xFF, 0xA6, 0, 0, 0, 0, 0}
	defaultB.ClockQuality = datatypes.ClockQuality{248, 0x2a, 0}

	var aMsgs []*general.AnnounceMessage
	aMsgs = make([]*general.AnnounceMessage, 10)
	var delay_RespMsgs []*general.Delay_RespMessage
	delay_RespMsgs = make([]*general.Delay_RespMessage, 10)
	var follow_upMsgs []*general.Follow_UpMessage
	follow_upMsgs = make([]*general.Follow_UpMessage, 10)
	var pdelay_resp_follow_upMsgs []*general.Pdelay_Resp_Follow_UpMessage
	pdelay_resp_follow_upMsgs = make([]*general.Pdelay_Resp_Follow_UpMessage, 10)

	for key, _ := range aMsgs {
		aMsgs[key] = new(general.AnnounceMessage)
		//aMsgs[key] = general.AnnounceMessage{}
		aMsgs[key].Init()
		aMsgs[key].SourcePortIdentity.ClockIdentity = datatypes.ClockIdentity{byte(key), 0xFF, 0xA5, 0, 0, 0, 0, 0}
	}
	aMsgs[1] = nil
	fmt.Println(aMsgs)
	for key, _ := range delay_RespMsgs {
		delay_RespMsgs[key] = new(general.Delay_RespMessage)
		//aMsgs[key] = general.Delay_RespMessage{}
		delay_RespMsgs[key].Init()
		delay_RespMsgs[key].ParentDS.ParentPortIdentity.ClockIdentity  = datatypes.ClockIdentity{byte(key), 0xFF, 0xA5, 0, 0, 0, 0, 0}
	}
	delay_RespMsgs[1] = nil
	fmt.Println(delay_RespMsgs)
	/*for key, _ := range follow_upMsgs {
		follow_upMsgs[key] = new(general.Follow_UpMessage)
		//aMsgs[key] = general.Follow_UpMessage{}
		follow_upMsgs[key].Init()
		follow_upMsgs[key].Timestamp = time.Now().Local()
	}*/
	follow_upMsgs[1] = nil
	fmt.Println(follow_upMsgs)
	for key, _ := range pdelay_resp_follow_upMsgs {
		pdelay_resp_follow_upMsgs[key] = new(general.Pdelay_Resp_Follow_UpMessage)
		//aMsgs[key] = general.Pdelay_Resp_Follow_UpMessage{}
		pdelay_resp_follow_upMsgs[key].Init()
		pdelay_resp_follow_upMsgs[key].ParentDS.ParentPortIdentity.ClockIdentity = datatypes.ClockIdentity{byte(key), 0xFF, 0xA5, 0, 0, 0, 0, 0}
	}
	pdelay_resp_follow_upMsgs[1] = nil
	fmt.Println(pdelay_resp_follow_upMsgs)

	fmt.Println("==========================================")
	portHandler := new(state.Port)
	portHandler.Init(nil, nil, nil, nil)

	for i := 0; i < 10; i++ {
		a := new(general.AnnounceMessage)
		a.Init()
		a.SourcePortIdentity.ClockIdentity = datatypes.ClockIdentity{0xFF, 0xFF, 0xA6, 0, 0, 0, 0, 0}
		a.ParentDS.GrandmasterClockQuality = datatypes.ClockQuality{248, 0x2a, 0}
		a.ParentDS.GrandmasterIdentity = datatypes.ClockIdentity{0xFF, 0xFF, 0xA6, 0, 0, 0, 0, 0}
		a.ParentDS.GrandmasterPriority1 = 10
		a.ParentDS.GrandmasterPriority2 = uint8(i)
		a.StepsRemoved = 10
		a.ParentDS.ParentPortIdentity.ClockIdentity = datatypes.ClockIdentity{0xFF, 0xFF, 0xA6, 0, 0, 0, 0, 0}
		a.ParentDS.ParentPortIdentity.PortNumber = 100
		portHandler.AddAnnounceMessage(*a)
	}
	//a, b := state.DataSetComparison(&portHandler.AnnounceMsgs[0], &portHandler.AnnounceMsgs[1])
	//fmt.Println(a,b)

	/*fmt.Printf("port: %+v, best: %+v\n", portHandler.AnnounceMsgs, portHandler.BestAnnounceMsg)
	err := portHandler.Compare()
	helper.ErrHandling(err)
	fmt.Printf("port: %+v, best: %+v\n", portHandler.AnnounceMsgs, portHandler.BestAnnounceMsg)*/

/*	fmt.Println("==========================================")
	c := make(chan string)
	go foo(c)
	go goo(c)
	time.Sleep(60*time.Second)
	fmt.Println("done")
	me := new(Me)
	fourChan := make(chan int)
	fmt.Println("Hi from line 145")
	go me.SendStuff(fourChan)
	fmt.Println("Hi from line 147")
	go me.ReceiveStuff(fourChan)
	fmt.Println("Hi from line 149")
	go me.Sleepy()
	fmt.Println("Hi from line 151")
	time.Sleep(60 * time.Second)
	fmt.Println("BORING! I'm out")*/
	fmt.Println("==========================")
	defaultDS := new(datasets.DefaultDS)
	defaultDS.Priority1 = 13
	defaultDS.Priority2 = 13
	defaultDS.ClockIdentity = datatypes.ClockIdentity{0xFF, 0xFF, 0xA5, 0, 0, 0, 0, 0}
	defaultDS.ClockQuality = datatypes.ClockQuality{248, 0x2a, 0}
	//cDS := new(datasets.CurrentDS)

	//go startPrintSome()
	//time.Sleep(60 * time.Second)
}
/*
func foo(s chan string) {
	for {
		fmt.Printf("Foo: %v\n", <-s)
		s<- fmt.Sprintf("from foo")
		time.Sleep(20 * time.Millisecond)
	}
}

func goo(s chan string) {
	for {
		fmt.Printf("Goo: %v\n", <-s)
		s<- fmt.Sprintf("from goo")
		time.Sleep(30 * time.Millisecond)
	}
}

func (m* Me) SendStuff(c chan int) {
	fmt.Println("In SendStuff()...")
	for i := 0; ; i++ {
		c <- i
		fmt.Printf("@SendLoop: %v\n", i)
		time.Sleep(10 * time.Millisecond)
	}
}

func (m* Me) ReceiveStuff(c chan int) {
	timer := time.After(500 * time.Millisecond)
	fmt.Println("In ReceiveSutff()...")
	for {
		select {
		case x := <-c:
			fmt.Println(x)
		case <-timer:
			fmt.Println("I'm done...")
			return
		}
	}
}
*/
/*
func printSome(c chan string) {
	fmt.Println("Starte Print Schleife")
	for {
		fmt.Println("ich drucke")
		time.Sleep(500 * time.Millisecond)
	}

}

func startPrintSome() {
	c := make(chan string)
	go printSome(c)
}
*/
/*
func (m* Me) Switch() {
	if m.compute {
		m.compute = false
	} else {
		m.compute = true
	}
}

func (m* Me) Sleepy() {
	fmt.Println("I'm Sleepy!")
	for {
		m.Switch()
		time.Sleep(500 * time.Millisecond)
	}
}

type Me struct {
	compute bool
}
*/
